package com.freemalll.merchant.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;

import java.util.HashMap;
import java.util.Map;

public class CommonUtil {

    public static boolean isJson(String str) {
        try {
            // 尝试解析为 JSON 对象
            JSON.parseObject(str);
            return true;
        } catch (JSONException e) {
            try {
                // 尝试解析为 JSON 数组
                JSON.parseArray(str);
                return true;
            } catch (JSONException ex) {
                return false;
            }
        }
    }
    public static long ChineseToArabicConverter(String chineseNumber) {
        Map<Character, Integer> CHINESE_NUMBERS = new HashMap<>();
        Map<Character, Integer> CHINESE_UNITS = new HashMap<>();
        CHINESE_NUMBERS.put('零', 0);
        CHINESE_NUMBERS.put('一', 1);
        CHINESE_NUMBERS.put('二', 2);
        CHINESE_NUMBERS.put('三', 3);
        CHINESE_NUMBERS.put('四', 4);
        CHINESE_NUMBERS.put('五', 5);
        CHINESE_NUMBERS.put('六', 6);
        CHINESE_NUMBERS.put('七', 7);
        CHINESE_NUMBERS.put('八', 8);
        CHINESE_NUMBERS.put('九', 9);
        CHINESE_NUMBERS.put('壹', 1);
        CHINESE_NUMBERS.put('贰', 2);
        CHINESE_NUMBERS.put('叁', 3);
        CHINESE_NUMBERS.put('肆', 4);
        CHINESE_NUMBERS.put('伍', 5);
        CHINESE_NUMBERS.put('陆', 6);
        CHINESE_NUMBERS.put('柒', 7);
        CHINESE_NUMBERS.put('捌', 8);
        CHINESE_NUMBERS.put('玖', 9);


        CHINESE_UNITS.put('十', 10);
        CHINESE_UNITS.put('百', 100);
        CHINESE_UNITS.put('千', 1000);
        CHINESE_UNITS.put('万', 10000);
        CHINESE_UNITS.put('亿', 100000000);
        CHINESE_UNITS.put('拾', 10);
        CHINESE_UNITS.put('佰', 100);
        CHINESE_UNITS.put('仟', 1000);
        CHINESE_UNITS.put('萬', 10000);

        long result = 0;
        long temp = 0;
        long unit = 1;

        for (int i = chineseNumber.length() - 1; i >= 0; i--) {
            char c = chineseNumber.charAt(i);
            if (CHINESE_UNITS.containsKey(c)) {
                unit = CHINESE_UNITS.get(c);
                if (unit >= 10000) {
                    result += temp * unit;
                    temp = 0;
                    unit = 1;
                }
            } else if (CHINESE_NUMBERS.containsKey(c)) {
                temp += CHINESE_NUMBERS.get(c) * unit;
                unit = 1;
            }
        }
        result += temp;

        return result;
    }
}
